package com.cn.task;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;

import org.springframework.jdbc.core.RowCallbackHandler;

import com.cn.xc.utils.DateUtil;

/**
 * FUN: 记录 to 文件
 *
 * @author sushy
 * @date 2016-3-23 
 */
public class RowToFile implements RowCallbackHandler{

	private String colSeparator;
	private String rowSeparator;
	private int columnAmount;
	
	private int rownum = 0;
	private OutputStreamWriter output;

	public RowToFile() {
		this.output = null;
		this.colSeparator = "|#%#|";
		this.rowSeparator = "\r\n";
		this.columnAmount = 0;
	}
	
	public RowToFile(OutputStreamWriter output) {
		this.output = output;
		this.colSeparator = "|#%#|";
		this.rowSeparator = "\r\n";
		this.columnAmount = 0;
	}
	
	public RowToFile(OutputStreamWriter output, String colSep, String rowSep) {
		this.output = output;
		this.colSeparator = colSep;
		this.rowSeparator = rowSep;
		this.columnAmount = 0;
	}
	
	public RowToFile(OutputStreamWriter output, String colSep, String rowSep, int amount) {
		this.output = output;
		this.colSeparator = colSep;
		this.rowSeparator = rowSep;
		this.columnAmount = amount;
	}
	
	public RowToFile(String colSep, String rowSep) {
		this.output = null;
		this.colSeparator = colSep;
		this.rowSeparator = rowSep;
		this.columnAmount = 0;
	}
	
	public RowToFile(String colSep, String rowSep, int amount) {
		this.output = null;
		colSeparator = colSep;
		rowSeparator = rowSep;
		columnAmount = amount;
	}
		
	public void setColSeparator(String colSeparator) {
		this.colSeparator = colSeparator;
	}

	public void setRowSeparator(String rowSeparator) {
		this.rowSeparator = rowSeparator;
	}

	public void setColumnAmount(int columnAmount) {
		this.columnAmount = columnAmount;
	}

	public int getRowNum() {
		return rownum;
	}

	public void processRow(ResultSet rs) throws SQLException {

		ResultSetMetaData md = rs.getMetaData();
		int cols = md.getColumnCount();
		if (columnAmount > 0 && cols > columnAmount) {
			cols = columnAmount;
		}

		for (int i = 1; i <= cols; i++) {
			if (i > 1) {
				write(colSeparator);
			}
			writeObject(rs.getObject(i));
		}
		write(rowSeparator);
		rownum++;

		// 每1000行flush一次
		if (rownum > 0 && rownum % 1000 == 0) {
			try {
				output.flush();
			} catch (IOException e) {
				throw new RuntimeException("写文件异常!", e);
			}
		}
	}


	/**
	 * 写入文本
	 * 
	 * @param text
	 */
	private void write(String text) {
		try {
			output.write(text);
		} catch (IOException e) {
			throw new RuntimeException("写文件异常!", e);
		}
	}

	/**
	 * 写入对象
	 * 
	 * @param obj
	 */
	private void writeObject(Object obj) {
		if (obj == null) {
			return;
		}
		if (obj instanceof String) {
			write((String) obj);
		} else if (obj instanceof Date) {
			write(DateUtil.date2Str((Date) obj));
		} else {
			write(obj.toString());
		}
	}


}
